# _*_ coding:utf-8 _*_
# @File  : datetime_handler.py
# @Time  : 2021-07-16  10:42
# @Author: zizle

import calendar
import datetime


def get_current_month_start_end(year, month):  # 获取指定月份的第一天和最后一天
    _, end_day = calendar.monthrange(year, month)
    month_start = datetime.datetime.strptime(f'{year}-{month}-01', '%Y-%m-%d')
    month_end = datetime.datetime.strptime(f'{year}-{month}-{end_day}', '%Y-%m-%d')
    return month_start, month_end


def get_current_quarter_start_end(date: datetime.datetime):  # 获取指定日期的本季度第一天和最后一天
    quarter = (date.month - 1) // 3 + 1
    if quarter == 1:
        s = datetime.datetime.strptime(f'{date.year}-01-01', '%Y-%m-%d')
        e = datetime.datetime.strptime(f'{date.year}-03-31', '%Y-%m-%d')
    elif quarter == 2:
        s = datetime.datetime.strptime(f'{date.year}-04-01', '%Y-%m-%d')
        e = datetime.datetime.strptime(f'{date.year}-06-30', '%Y-%m-%d')
    elif quarter == 3:
        s = datetime.datetime.strptime(f'{date.year}-07-01', '%Y-%m-%d')
        e = datetime.datetime.strptime(f'{date.year}-09-30', '%Y-%m-%d')
    else:
        s = datetime.datetime.strptime(f'{date.year}-10-01', '%Y-%m-%d')
        e = datetime.datetime.strptime(f'{date.year}-12-31', '%Y-%m-%d')
    return s, e

